import state from "../state"

// 程序中临时使用 不保存到本地
export interface TempState {
    shopcart: {
        selectedGoodsIds: Set<string>,
        isSelectedAll: boolean
    },

    tabbar: {
        selectedIndex: number
    }
}

export default {
    namespace: "temp",
    state: {
        shopcart: {
            selectedGoodsIds: new Set<string>(),
        },
        tabbar: {
            selectedIndex: 0
        }
    },
    reducers: {
        shopcartInsertSelectedGoodsId (state: TempState, { payload }) {
            console.log('payload',payload)
            let set = state.shopcart.selectedGoodsIds
            if (payload instanceof Array) {
                for (const id of payload) {
                    set.add(id)
                }
            } else {
                set.add(payload)
            }

            console.log('set',set)
    
            return { ...state, ...{ 
                shopcart: {
                    selectedGoodsIds: set
                }
             }}
        },

        shopcartRemoveSelectedGoodsId(state: TempState, { payload }) {
            let set = state.shopcart.selectedGoodsIds
            if (payload instanceof Array) {
                for (const id of payload) {
                    set.delete(id)
                }
            } else {
                set.delete(payload)
            }
    
            return { ...state, ...{ 
                shopcart: {
                    selectedGoodsIds: set
                }
             }}
        },

        shopcartClearSelectedGoodsId() {
            return { ...state, ...{ shopcart: {
                selectedGoodsIds: new Set<string>()
            }}}
        },

        tabbarSetSelectedIndex(state: TempState, { payload }) {
            return { ...state, ...{ tabbar: {
                selectedIndex: payload
            }}}
        }
    }
}